package com.dsy.xiangcun_manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dsy.tools.common.utils.AliyunOSSUtil;
import com.dsy.tools.common.utils.QueryObj;
import com.dsy.tools.entity.TbPhoto;
import com.dsy.tools.entity.TbTou;
import com.dsy.xiangcun_manage.service.ITbPhotoService;
import com.dsy.xiangcun_manage.service.ITbTouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dsy
 * @since 2021-03-07
 */
@Controller
@RequestMapping("photo")
public class TbPhotoController {
    @Autowired
    ITbPhotoService photoService;
    @Autowired
    ITbTouService stuService;
    /**
     * 跳转到风景照发布界面
     * @return
     */


    @RequestMapping("/addPhoto")
    public String addPhoto(){
        return "/photo/photo-add";
    }
    @RequestMapping("/uploadPhotoImg")
    @ResponseBody
    public Map<String,Object> uploadPhotoImg(@RequestParam("imgFile") List<MultipartFile> imgFiles){
        Map<String, Object> map = new HashMap<>();
        List<String> data = new ArrayList<>();
        for (MultipartFile imgFile : imgFiles) {
            String filename = imgFile.getOriginalFilename();
            System.out.println(filename);
            try {
                if (imgFile != null) {
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(filename);
                        FileOutputStream os = new FileOutputStream(newFile);
                        os.write(imgFile.getBytes());
                        os.close();
                        imgFile.transferTo(newFile);
                        // 阿里云上传到OSS
                        String uploadUrl = AliyunOSSUtil.upLoad(newFile);
                        newFile.delete();
                        data.add(uploadUrl);
                    }
                    //
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        map.put("error",0);
        map.put("data",data);
        return map;
    }
    /**
     * 添加风景照信息
     * @param
     * @return
     */
    @RequestMapping("/savePhoto")
    public String savePhoto(TbPhoto photo, HttpSession session){
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        photo.setPhotoUser(tbTou.getUserId());
        photo.setPhotoTime(LocalDateTime.now());
        photoService.saveOrUpdate(photo);
        return "redirect:/photo/getAllMyPhoto";
    }
    /**
     * 查看当前登录用户发布的
     * @param m
     * @param page
     * @param limit
     * @param queryObj
     * @return
     */
    @RequestMapping("/getAllMyPhoto")
    public String getAllPhoto(Model m, @RequestParam(defaultValue = "1") long page, @RequestParam(defaultValue = "10") long limit, QueryObj queryObj, HttpSession session){

        QueryWrapper<QueryObj> wrapper = new QueryWrapper<>();
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        queryObj.setTid(tbTou.getUserId());
        wrapper.setEntity(queryObj);

        IPage<TbPhoto> photoPage = photoService.getAllPhoto(page,limit,wrapper);
        m.addAttribute("photoPage",photoPage);
        m.addAttribute("page",page);
        m.addAttribute("queryObj",queryObj);
        return "/photo/photo-base";
    }
    /**
     * 根据风景照Id查询
     * @param m
     * @param photoId
     * @return
     */
    @RequestMapping("/getPhotoById")
    public String getPhotoById(Model m,Integer photoId){
        TbPhoto photo=photoService.getPhotoById(photoId);
        m.addAttribute("photo",photo);
        return "/photo/photo-edit";
    }
    /**
     * 修改更新风景照
     * @param tbPhoto
     * @return
     */
    @RequestMapping("/updatePhoto")
    public String updatePhoto(TbPhoto tbPhoto){
        tbPhoto.setPhotoTime(LocalDateTime.now());
        photoService.updateById(tbPhoto);
        return "redirect:/photo/getAllMyPhoto";
    }

    /**
     * 删除风景照
     * @param PhotoIds
     * @return
     */
    @RequestMapping("/delPhotoById")
    public String delPhotoById(@RequestParam("photoId") List<Integer> PhotoIds){
        photoService.removeByIds(PhotoIds);
        return "redirect:/photo/getAllMyPhoto";
    }

}
